var solarTerm = new Array(
  '小寒',
  '大寒',
  '立春',
  '雨水',
  '惊蛰',
  '春分',
  '清明',
  '谷雨',
  '立夏',
  '小满',
  '芒种',
  '夏至',
  '小暑',
  '大暑',
  '立秋',
  '处暑',
  '白露',
  '秋分',
  '寒露',
  '霜降',
  '立冬',
  '小雪',
  '大雪',
  '冬至'
)
var sTermInfo = new Array(
  0,
  21208,
  42467,
  63836,
  85337,
  107014,
  128867,
  150921,
  173149,
  195551,
  218072,
  240693,
  263343,
  285989,
  308563,
  331033,
  353350,
  375494,
  397447,
  419210,
  440795,
  462224,
  483532,
  504758
)
/**
 * 某年的第n个节气为几日(从0小寒起算)
 * @param {*} y
 * @param {*} n
 * @returns
 */
function sTermDate(y, n) {
  return new Date(
    31556925974.7 * (y - 1900) +
      sTermInfo[n] * 60000 +
      Date.UTC(1900, 0, 6, 2, 5)
  )
}
/**
 * 获取yyyy年第num个节气小寒的公历日期
 * @param {*} yyyy 
 * @param {*} num 
 * @returns 
 */
function getJqDate(yyyy, num) {
  var sterm = sTermDate(yyyy, num)
  return (
    sterm.getFullYear() +
    '-' +
    padNumber(sterm.getMonth() + 1) +
    '-' +
    padNumber(sterm.getUTCDate())
  )
}
/**
 * 获取某年所有的节气
 * @param {*} yyyy 
 * @returns 
 */
function getJq(yyyy) {
  let list = []
  for (let index = 0; index < 24; index++) {
    let currentDay = getJqDate(yyyy, index)
    let result = compareTime(currentDay)
    list.push({
      name: solarTerm[index],
      date: currentDay,
      status: result.status,
      label: result.label
    })
  }
  return list
}

/**
 * 获取当前时间
 * @param {*} args 
 * @param {*} format 
 * @returns 
 */
function getCurrentTime(args, format = true) {
  const now = new Date()
  const year = now.getFullYear()
  const month = format ? padNumber(now.getMonth() + 1) : now.getMonth() + 1
  const day = format ? padNumber(now.getDate()) : now.getDate()
  const hours = format ? padNumber(now.getHours()) : now.getHours()
  const minutes = format ? padNumber(now.getMinutes()) : now.getMinutes()
  const seconds = format ? padNumber(now.getSeconds()) : now.getSeconds()
  if (args === 'yyyy') {
    return `${year}`
  } else if (args === 'MM') {
    return `${month}`
  } else if (args === 'dd') {
    return `${day}`
  } else if (args === 'yyyy-MM-dd') {
    return `${year}-${month}-${day}`
  }
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}
function padNumber(num) {
  return num < 10 ? '0' + num : num
}

/**
 * 两个日期比较
 * @param {*} compareDay 
 * @returns 
 */
function compareTime(compareDay) {
  const curDate = new Date()
  const year = curDate.getFullYear()
  const month = padNumber(curDate.getMonth() + 1)
  const day = padNumber(curDate.getDate())
  const curDay = `${year}-${month}-${day}`
  const date2 = new Date(compareDay)
  let result = {}
  if (curDate.getTime() > date2.getTime()) {
    result.status = false
    result.label = '已过'
  } else {
    result.status = true
    result.label = daysBetween(curDay, compareDay)+'天'
  }
  return result
}

/**
 * 计算2个日期之间天数
 * @param {*} date1 
 * @param {*} date2 
 * @returns 
 */
function daysBetween(date1, date2) {
  let oneDay = 24*60*60*1000; // 一天的毫秒数
  let firstDate = new Date(date1);
  let secondDate = new Date(date2);
 
  // 将两个日期都设置为午夜（00:00:00）以避免时差影响
  firstDate.setHours(0, 0, 0, 0);
  secondDate.setHours(0, 0, 0, 0);
 
  // 计算两个日期之间的毫秒差
  let diff = Math.abs(firstDate - secondDate);
 
  // 将毫秒差转换为天数
  return Math.floor(diff / oneDay);
}

export default {
  getJq,
  getCurrentTime,
}
